<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="68px">
        <el-form-item label="工段">
          <el-select
            v-model="searchParam.workshopSectionId"
            placeholder="请选择工段"
            clearable
            @keyup.enter="search"
            filterable
          >
            <el-option
              v-for="item in workshopSectionList"
              :key="item.id"
              :label="`${item.workshopSectionCode}`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="工序">-->
        <!--          <el-select-->
        <!--            v-model="searchParam.processId"-->
        <!--            placeholder="请选择工序"-->
        <!--            clearable-->
        <!--            @keyup.enter="search"-->
        <!--            filterable-->
        <!--            @change="changeProcess"-->
        <!--          >-->
        <!--            <el-option-->
        <!--              v-for="item in processList"-->
        <!--              :key="item.id"-->
        <!--              :label="`${item.processCode}`"-->
        <!--              :value="item.id"-->
        <!--            />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!-- <el-form-item label="责任人">
            <el-select
              v-model="searchParam.responsibleRoleList"
              placeholder="请选责任人"
              clearable
              @keyup.enter="search"
            >
              <el-option
                v-for="item in responsibleList"
                :key="item.id"
                :label="`${item.processCode}`"
                :value="item.id"
              />
            </el-select>
          </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header v-if="authAllPerm([`${props.type}:qxzr:add`])">
        <el-button
          v-if="authAllPerm([`${props.type}:qxzr:add`])"
          type="success"
          plain
          @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :maxHeight="tableHeight"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          label="工段"
          prop="workshopSectionCode"
          width="120"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="邮件接收人"
          prop="noticeRoleList"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工单查看人"
          prop="viewRoleList"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="责任人"
          prop="responsibleRoleList"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="审批人"
          prop="leaderRoleList"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="最终审批人"
          prop="approverRoleList"
          min-width="60"
          show-overflow-tooltip
          align="center"
        />
        <!--        <el-table-column-->
        <!--          label="邮件发送"-->
        <!--          prop="enabled"-->
        <!--          width="76"-->
        <!--          show-overflow-tooltip-->
        <!--          align="center"-->
        <!--        >-->
        <!--          <template #default="scope">-->
        <!--            <el-tag v-if="scope.row.isSendMail == 1">是</el-tag>-->
        <!--            <el-tag type="info" v-else>否</el-tag>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column fixed="right" label="操作" width="110" align="center">
          <template #default="scope">
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="查看"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialogdetail(scope.row)"
              >
                <i-ep-search />
              </el-button>
            </el-tooltip>

            <el-tooltip
              :hide-after="0"
              effect="light"
              content="编辑"
              placement="top"
              v-if="authAllPerm([`${props.type}:qxzr:edit`])"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialog(scope.row)"
              >
                <i-ep-edit />
              </el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="删除"
              placement="top"
              v-if="authAllPerm([`${props.type}:qxzr:delete`])"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="deleteItem(scope.row.id)"
              >
                <i-ep-delete />
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <CommonDutySetting
      :type="props.type"
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import {
  useTable,
  useDictionary,
  useComputeTableHeight,
} from "@/utils/useTable";
import request from "@/utils/request";
import CommonDutySetting from "@/views/psm/common/commonDutySetting.vue";
import { authAllPerm } from "@/utils/index";

defineOptions({
  name: "PSMCOMMONDUTYLIST",
  inheritAttrs: false,
});

const props = defineProps({
  type: {
    type: String,
    default: () => {
      return "psm";
    },
  },
});

// 请求参数
const requestApi = `/dmpDosPcm/${props.type}processduty/queryByPage`;

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  processId: "",
  workshopSectionId: "",
};

const deleteParam = {
  api: `/dmpDosPcm/${props.type}processduty/delete`,
  data: {},
};

// 数据请求错误后处理
function requestError() {}

// 定义tableHeight, 将计算注入dataCallBack回调中
const tableHeight = ref();
function dataCallBack(data: any) {
  setTimeout(() => {
    tableHeight.value = useComputeTableHeight();
  }, 100);
  return data;
}

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/dmpDosPcm/${props.type}processduty/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开工序责任设置表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "监控权限及责任设定-修改";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "监控权限及责任设定-新增";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
/** 打开工序责任设置表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "监控权限及责任设定-详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/dmpDosPcm/${props.type}processduty/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}

const processList = ref([]);
const getProcessList = async () => {
  const { data } = await request({
    url: `/mdm/mdmprocess/getList`,
    method: "post",
    data: {},
  });
  processList.value = data || [];
};
const workshopSectionList = ref([]);
const getWorkshopSectionList = async () => {
  const { data } = await request({
    url: `/mdm/mdmworkshopsection/getList`,
    method: "post",
    data: {},
  });
  workshopSectionList.value = data || [];
};

// 当工序改变
// 责任人列表
const responsibleList = ref([]);
const changeProcess = async () => {
  await request({
    url: `/mdm/mdmprocess/getList`,
    method: "post",
    data: { processId: searchParam.value.processId },
  }).then((res) => {
    responsibleList.value = res.data;
    console.log("责任人列表", responsibleList.value);
  });
};

onMounted(() => {
  // getProcessList();
  getWorkshopSectionList();
});
</script>
